package com.study.talk.app.friend;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.widget.SearchView;

import com.study.talk.adapter.UserListAdapter;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.databinding.ActivityAddFriendBinding;
import com.study.talk.entity.UserInfo;
import com.study.talk.entity.resp.QueryUserListResponse;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;

import java.util.List;

import io.netty.util.internal.StringUtil;

public class AddFriendActivity extends BaseActivity {


    private List<UserInfo> userInfoList;

    ActivityAddFriendBinding activityAddFriendBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityAddFriendBinding = ActivityAddFriendBinding.inflate(getLayoutInflater());
        setContentView(activityAddFriendBinding.getRoot());
        setSupportActionBar(activityAddFriendBinding.addFriendToolbar);
        initView();
    }


    private void initView() {

        activityAddFriendBinding.addFriendToolbar.setNavigationOnClickListener(v -> finish());

        activityAddFriendBinding.addFriendSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (StringUtil.isNullOrEmpty(query)){
                    ToastUtil.showToast(AddFriendActivity.this,"请输入搜索内容");
                    return false;
                }
                OkHttp.get("/userInfo/get/list/"+query,res -> {
                    QueryUserListResponse response = GsonInstance.instance.fromJson(res, QueryUserListResponse.class);
                    if (response.getStatus() == 200){
                        if (response.getData() == null || response.getData().isEmpty()) {
                            runOnUiThread(()-> ToastUtil.showToast(AddFriendActivity.this,"暂无符合条件的用户..."));
                        }else {
                            userInfoList = response.getData();
                            UserListAdapter adapter = new UserListAdapter(userInfoList);
                            runOnUiThread(()->{
                                activityAddFriendBinding.addFriendListview.setAdapter(adapter);
                                activityAddFriendBinding.addFriendListview.setOnItemClickListener((adapterView, view, i, l) -> {
                                    Intent intent = new Intent(AddFriendActivity.this, UserInfoActivity.class);
                                    intent.putExtra("userId",userInfoList.get(i).getId());
                                    startActivity(intent);
                                });
                            });
                        }
                    }
                });
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }
}